#include "nnslite.h"


/** nnsliteStore  */
NNSLITE_RET nnsliteStoreCreate(nnsliteStore* store, const char* name, nnsliteDistFunc func)
{
	strcpy(store->name, name);
	store->distfunc = func;
	return nnsliteMTreeInit(&store->mtree, func);
}

NNSLITE_RET nnsliteStoreFree(nnsliteStore* store)
{
	return nnsliteMTreeFree(&store->mtree);
}

NNSLITE_RET nnsliteStoreOpen(nnsliteStore* store, const char* filename)
{
	s32 ret;
	FILE* fp = fopen(filename, "rb");
	if (fp == NULL)
		return NNSLITE_RET_ERR_FILE_CANNOT_OPEN;
	fread(store->name, NNSLITE_MAX_NAMELEN, 1, fp);
	
	ret = nnsliteMTreeRead(&store->mtree, fp);
	NNSLITE_ON_FAILED_RETURN(ret);

	fclose(fp);
	return NNSLITE_RET_OK;
}

NNSLITE_RET nnsliteStoreSave(nnsliteStore* store, const char* filename)
{
	s32 ret;
	FILE* fp = fopen(filename, "wb");
	if (fp == NULL)
		return NNSLITE_RET_ERR_FILE_CANNOT_OPEN;
	fwrite(store->name, NNSLITE_MAX_NAMELEN, 1, fp);
	
	ret = nnsliteMTreeWrite(&store->mtree, fp);
	NNSLITE_ON_FAILED_RETURN(ret);

	fclose(fp);
	return NNSLITE_RET_OK;
}

NNSLITE_RET nnsliteStoreNNSearch(nnsliteStore* store, const nnsliteObject* object, u32 top, nnsliteObject* results)
{
	return nnsliteMTreeNNSearch(&store->mtree, object, top, results);
}

NNSLITE_RET nnsliteStorePrint(nnsliteStore* store, FILE* fp)
{
	return nnsliteMTreePrint(&store->mtree, fp);
}

NNSLITE_RET nnsliteStoreInsert(nnsliteStore* store, nnsliteObject* object)
{
	return nnsliteMTreeInsert(&store->mtree, object);
}